package com.ktgj.flight.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ktgj.file.api.RemoteFilesService;
import com.ktgj.flight.domain.FlightOrderChange;
import com.ktgj.flight.mapper.AirlinerMapper;
import com.ktgj.flight.mapper.FlightOrderChangeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.FlightOrderChangeTicketsMapper;
import com.ktgj.flight.domain.FlightOrderChangeTickets;
import com.ktgj.flight.service.IFlightOrderChangeTicketsService;

/**
 * 机票预订_机票退改签订单机票信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@Service
public class FlightOrderChangeTicketsServiceImpl implements IFlightOrderChangeTicketsService 
{
    @Autowired
    private FlightOrderChangeTicketsMapper flightOrderChangeTicketsMapper;
    @Autowired
    private FlightOrderChangeMapper changeMapper;
    @Autowired
    private AirlinerMapper airlinerMapper;
    @Autowired
    private RemoteFilesService remoteFilesService;

    /**
     * 查询机票预订_机票退改签订单机票信息
     * 
     * @param changeTicketId 机票预订_机票退改签订单机票信息ID
     * @return 机票预订_机票退改签订单机票信息
     */
    @Override
    public FlightOrderChangeTickets selectFlightOrderChangeTicketsById(Long changeTicketId)
    {
        return flightOrderChangeTicketsMapper.selectFlightOrderChangeTicketsById(changeTicketId);
    }

    /**
     * 查询机票预订_机票退改签订单机票信息列表
     * 
     * @param flightOrderChangeTickets 机票预订_机票退改签订单机票信息
     * @return 机票预订_机票退改签订单机票信息
     */
    @Override
    public List<FlightOrderChangeTickets> selectFlightOrderChangeTicketsList(FlightOrderChangeTickets flightOrderChangeTickets)
    {
        return flightOrderChangeTicketsMapper.selectFlightOrderChangeTicketsList(flightOrderChangeTickets);
    }

    /**
     * 新增机票预订_机票退改签订单机票信息
     * 
     * @param flightOrderChangeTickets 机票预订_机票退改签订单机票信息
     * @return 结果
     */
    @Override
    public int insertFlightOrderChangeTickets(FlightOrderChangeTickets flightOrderChangeTickets)
    {
        return flightOrderChangeTicketsMapper.insertFlightOrderChangeTickets(flightOrderChangeTickets);
    }

    /**
     * 修改机票预订_机票退改签订单机票信息
     * 
     * @param flightOrderChangeTickets 机票预订_机票退改签订单机票信息
     * @return 结果
     */
    @Override
    public int updateFlightOrderChangeTickets(FlightOrderChangeTickets flightOrderChangeTickets)
    {
        return flightOrderChangeTicketsMapper.updateFlightOrderChangeTickets(flightOrderChangeTickets);
    }

    /**
     * 批量删除机票预订_机票退改签订单机票信息
     * 
     * @param changeTicketIds 需要删除的机票预订_机票退改签订单机票信息ID
     * @return 结果
     */
    @Override
    public int deleteFlightOrderChangeTicketsByIds(Long[] changeTicketIds)
    {
        return flightOrderChangeTicketsMapper.deleteFlightOrderChangeTicketsByIds(changeTicketIds);
    }

    /**
     * 删除机票预订_机票退改签订单机票信息信息
     * 
     * @param changeTicketId 机票预订_机票退改签订单机票信息ID
     * @return 结果
     */
    @Override
    public int deleteFlightOrderChangeTicketsById(Long changeTicketId)
    {
        return flightOrderChangeTicketsMapper.deleteFlightOrderChangeTicketsById(changeTicketId);
    }

    //退票订单详情
    @Override
    public List<FlightOrderChangeTickets> refundFlightOrder(String orderId) {
        List<FlightOrderChangeTickets> orderChangeTickets = new ArrayList<>();
        List<FlightOrderChange> orderChange = changeMapper.queryFlightOrderChangeByOrderId(orderId);
        orderChange.forEach(item -> {
            if (item.getOldChangeOrderId() == null) {
                for (FlightOrderChangeTickets tickets : flightOrderChangeTicketsMapper.refundFlightOrder(item.getChangeOrderId())) {
                    String airlineName = airlinerMapper.queryAirliner(tickets.getAirliner());
                    tickets.setAirlineName(airlineName);

                    String logo = remoteFilesService.airlineLogo();
                    tickets.setAirlineLogo(logo + tickets.getAirliner() + ".gif");
                    orderChangeTickets.add(tickets);
                }
            }
        });
        return orderChangeTickets;
    }

    @Override
    public FlightOrderChangeTickets queryOrderChangeTickets(String ticketNo) {
        return flightOrderChangeTicketsMapper.queryOrderChangeTickets(ticketNo);
    }

    @Override
    public FlightOrderChangeTickets queryTicketsByPNR(String pnr) {
        return flightOrderChangeTicketsMapper.queryTicketsByPNR(pnr);
    }

    //定时任务通过PNR查询订单详情
    @Override
    public FlightOrderChangeTickets selectOrderChangeTickets(String pnr) {
        return flightOrderChangeTicketsMapper.selectOrderChangeTickets(pnr);
    }

    /**
     * 查询改签订单详情
     * @param orderId
     * @return
     */
    @Override
    public List<FlightOrderChangeTickets> queryChangeTicketsByOrderId(Long orderId) {
        return flightOrderChangeTicketsMapper.queryChangeTicketsByOrderId(orderId);
    }
}
